function renderTable(obj,result) {
	var wid = $(obj).attr('wid')
	var disObj = {}
	disObj[wid] = result.chartSource
	//console.log(disObj)

	var thead = '<thead><tr style="background-color:#d4e3e5;">'
	for (var i=0;i<result.chartSource.dataset.dimensions.length;i++) {
		thead +='<th style="border-width: 1px;padding: 8px;border-style: solid;border-color: #a9c6c9;">'+result.chartSource.dataset.dimensions[i]+'</th>'
	}
	thead +='</tr></thead>'
	
	var tbody = '<tbody>'
	for (var i=0;i<result.chartSource.dataset.source.length;i++) {
		tbody += '<tr style="background-color:">'
		for (var j=0;j<result.chartSource.dataset.dimensions.length;j++) {
			var fieldName = result.chartSource.dataset.dimensions[j]
			tbody +='<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #a9c6c9;">'+result.chartSource.dataset.source[i][fieldName]+'</td>'
		}
		tbody +='</tr>'
	}
	tbody +='</tbody>'

	/*var temp = '<table class="table table-striped table-bordered" '+
	'style=""width:100%;font:90%/1.45em "Helvetica Neue", HelveticaNeue, Helvetica, Arial, sans-serif !important">'+thead+tbody+'</table>'
	*/
	var temp = '<table style="font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;'+
	'border-color: #a9c6c9;border-collapse: collapse;" >'+thead+tbody+'</table>'
	obj.html(temp)
	
	//$(obj).click(function(){
	//  	console.log(disObj)
	//});
}


function renderTemplate(obj,result) {
	var disObj = {}
	var wid = $(obj).attr('wid')
	disObj[wid] = result.chartSource
	//console.log(disObj)
	var tpl = $('script',obj).html()
	var html = baidu.template(tpl,
		{
		    rows:result.chartSource.dataset.source,
		    cols:result.chartSource.dataset.dimensions
		}
	);  
	$(obj).html(html)

	//$(obj).click(function(){
	//  	console.log(disObj)
	//});
}


function renderChart(obj,result) {
	var wid = $(obj).attr('wid')
	var id = $(obj).attr('id')
	var disObj = {}
	disObj[wid] = result.chartSource
	//console.log(disObj)
	if (result.themeName!=null && result.themeName!="") {
		//console.log(JSON.parse(result.themeSource))
		echarts.registerTheme(result.themeName, JSON.parse(result.themeSource))
		var chart = echarts.init(obj,result.themeName);
		eval("var myOption = "+result.chartSource)
    	chart.setOption(myOption)
        //chart.setOption(result.chartSource)
	} else {
		//console.log(obj)
		var chart = echarts.init(obj);
		eval("var myOption = "+result.chartSource)
    	chart.setOption(myOption)
    }

    if (id!=null && id!="") {
	    for (var i=0;i<eventArray.length;i++) {
	    	var temp = id+"On"+eventArray[i].charAt(0).toUpperCase()+eventArray[i].substring(1)
	    	if (window[temp]!=null) {
	    		chart.on(eventArray[i], function (params) {
			    	try {
			    		eval(this._dom.id+"On"+params.event.type.charAt(0).toUpperCase()+params.event.type.substring(1)+"(params)")
			    	} catch (e) {
			    		console.log(e)
			    	}
			   	})
		    }
	    	
	   	}
	   	window[id] = chart
	   	window[id].load = function(params,callback) {
	   		//console.log(JSON.stringify(params, null, ""))
	   		$.ajax({
				url:widgetServer+'/chart/render',
		        dataType:'jsonp',
		        //jsonp: "callback",
		        jsonpCallback: "success_callback",
		        data:{
		            wid:wid,
		            wp:params==null?null:JSON.stringify(params, null, "")
		        },
		        success:function(result){
		        	console.log(wid)
		        	if (result.success) {
		        		if ($(widgetObject[wsid]).attr('template')!=null&&$(widgetObject[wsid]).attr('template')=="true") {
			        		renderTemplate(widgetObject[wsid],result)
						} else {
							if ($(widgetObject[wsid]).attr('wtype')!=null&&$(widgetObject[wsid]).attr('wtype')=="table") {
								renderTable($(widgetObject[wsid]),result)
							} else {
								chart.setOption(result.chartSource)
							}
						}
			        } else {
			        	$(widgetObject[wsid]).html('无此组件:'+wid)
			        	$(widgetObject[wsid]).css('border','1px solid red')
			        }
			        if (callback!=null) {
		        		callback()
		        	}
		        }
		    })
	   	}
   }

   

   	/*chart.on("click", function (params) {
		console.log(33)
	})


   	chart.on("click", function (params) {
		console.log(33)
	})*/

	//$(obj).click(function(){
	//  	console.log(disObj)
	//});

	
}



function getWidget(widgets) {
	//console.log(widgets)
	    
	if (widgets.length!=0) {
		var wid = $(widgets[0]).attr('wid')
		var wp = $(widgets[0]).attr('wp')
		eval('wp='+wp)
		wp = JSON.stringify(wp, null, "")
		
		var wsid = $(widgets[0]).attr('wsid')
		$.ajax({
			url:widgetServer+'/chart/render',
	        dataType:'jsonp',
	        //jsonp: "callback",
	        jsonpCallback: "success_callback",
	        data:{
	            wid:wid,
	            wp:wp
	        },
	        success:function(result){
	        	//console.log(result)
	        	loadedWidget++
				$('#progressIndiator').html(parseInt(loadedWidget*100/totalWidget)+"%")

	        	if (result.success) {
	        		//alert(wid+" "+$(widgetObject[wsid]).attr('widgetType'))
		        	if ($(widgetObject[wsid]).attr('template')!=null&&$(widgetObject[wsid]).attr('template')=="true") {
		        		renderTemplate(widgetObject[wsid],result)
					} else {

						if ($(widgetObject[wsid]).attr('wtype')!=null&&$(widgetObject[wsid]).attr('wtype')=="table") {
							renderTable($(widgetObject[wsid]),result)
						} else {
							renderChart(widgetObject[wsid],result)
						}

						
					}
		        } else {
		        	$(widgetObject[wsid]).html('无此组件:'+wid)
		        	$(widgetObject[wsid]).css('border','1px solid red')
		        }
	        	widgets.splice(0,1)
		        getWidget(widgets)
	        } ,
	        error:function(err){
	        	console.log(err)
	        	loadedWidget++
				$('#progressIndiator').html(parseInt(loadedWidget*100/totalWidget)+"%")

	        	widgets.splice(0,1)
	        	getWidget(widgets)
	        }
	    });

    } else {
    	$('body').show()
    	$('#loadingIndiator').remove()
    }

}

var widgetObject = {}

var totalWidget = 0

var loadedWidget = 0

var wsid = 0

var eventArray = ["click","dblclick","mousedown","mousemove","mouseup","mouseover","mouseout"]

$(document).ready(function(){
	$('body').before("<span id='loadingIndiator'>正在读取数据....<span id='progressIndiator'></span></span>")
	$('#loadingIndiator').show()

	var widgetsArray = []
	var widgets = $("[wid]")
	totalWidget = widgets.length
    for(var i=0;i<widgets.length;i++) {
    	$(widgets[i]).attr('wsid',wsid)
	    widgetsArray.push($(widgets[i]))
	    //console.log($(widgets[i]).attr('wid'))
	    //widgetObject[$(widgets[i]).attr('wid')] = widgets[i]
	    widgetObject[wsid+''] = widgets[i]
	    wsid++
	}

	//console.log(widgetObject)
	

    getWidget(widgetsArray)
    //var widgets = $("[attribute='token']")
    
});